package top.starcatmeow.encryptedchat;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button loginbtn;
    TextInputEditText et1, et2, et3, et4;
    static ActionProcesser ap;
    SharedPreferences settings;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        settings = getSharedPreferences("serverinfo", 0);
        editor = settings.edit();

        setTitle("连接服务器");
        loginbtn = findViewById(R.id.button);
        et1 = findViewById(R.id.serverip);
        et2 = findViewById(R.id.serverport);
        et3 = findViewById(R.id.username);
        et4 = findViewById(R.id.password);
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String host = et1.getText().toString(), port = et2.getText().toString(), username = et3.getText().toString(), password = et4.getText().toString();
                editor.putString("host", host);
                editor.putString("port", port);
                editor.putString("username", username);
                editor.putString("password", password);
                editor.commit();
                Intent intent = new Intent(MainActivity.this, ChatActivity.class);
                startActivity(intent);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ap = new ActionProcesser();
                        ap.connect(host, port, username, password);
                    }
                }).start();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        et1.setText(settings.getString("host", ""));
        et2.setText(settings.getString("port", ""));
        et3.setText(settings.getString("username", ""));
        et4.setText(settings.getString("password", ""));
    }
}
